#!/bin/bash

BMC_ROOT_PATH='/scriptsdata/soft/bios_bmc_init/'

LOG_FILE='/tmp/init_bmc'
IPMI_USER='stonedev'
IPMI_PASS='Stone888'

INIT_SUPPORTLIST_LENOVO=("RD340X" "RD350X" "RD450X" "HR650X" "HR630X")
INIT_SUPPORTLIST_INSPUR=("NF5180M4" "NF5280M4" "NF5588M4" "NF5588M4" "SA5112M4" "SA5112M5" "SA5212M4" "SA5212M5")
INIT_SUPPORTLIST_SUGON=("A620-G30")
INIT_SUPPORTLIST_INVENTEC=("RM760-IV")

function ipmi_add_user(){

	channel_id=0
	for i in {1..10};
	do
		ipmitool user list $i 2>1 >/dev/null
		if [ $? -eq 0 ];then
			channel_id=$i
			break
		fi
	done

	if [ $channel_id -eq 0 ];then
		echo "Unable to get ipmi user channel"  | tee -a $LOG_FILE 
		exit 1
	fi

	ipmitool user set name 8 $IPMI_USER 
	ipmitool user set password 8 $IPMI_PASS
	ipmitool user priv 8 4 $channel_id
	ipmitool channel setaccess $channel_id 8 callin=on ipmi=on link=on privilege=4 
	ipmitool user enable 8
}

function ipmi_getIP(){

	ipmitool lan print  |grep 'IP Address' |grep -v 'Source' |awk -F':' '{print $2}' | awk '$1=$1'
}

function checkInitStatus(){

	if [ $1 -eq 0 ];then
		echo "BIOS and BMC initialization failed" | tee -a $LOG_FILE
		exit 1
	else
		echo "BIOS and BMC initialized successfully" |  tee -a $LOG_FILE
		exit 0
	fi
}

##################################
## check Lenovo Support init Model
##################################
function getProductModel(){

	arry=("$@")
	productName=${arry[0]}

	for (( i=1; i < ${#arry[*]}; i++ ));
	do
		model=${arry[i]}
		echo $productName |grep $model >/dev/null 
		if [ $? -eq 0 ];then
			echo $model
			return 
		fi
	done
	echo ""
	return 
}
############
## INSPUR
## NF5180M4
############
function init_INSPUR_NF5180M4()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/NF5180M4/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE


	ipmitool raw 0x3c 0x3a 0x1e 0x01 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	echo y | ./socflash_x64 if=./SA5112M4_BMC_4.23.0_20181109  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	./afulnx_64 SA5112M4_BIOS_4.1.17_20181204.bin /p /b /n /x /k /l /me  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	ipmitool sel clear | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status
}

############
## INSPUR
## NF5280M4
############
function init_INSPUR_NF5280M4()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/NF5280M4/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE


	echo y | ./socflash_x64 if=./NF5280M4_BMC_4.24.0_Tencent_20170119 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	./step1.sh NF5280M4_BIOS_4.1.12_Tencent-2U_20170207.bin  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}

############
## INSPUR
## NF5588M4
############
function init_INSPUR_NF5588M4()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/NF5588M4/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE


	./sum_2.5.2_Linux_x86_64/sum -c LoadDefaultBiosCfg | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	./sum_2.5.2_Linux_x86_64/sum -c LoadDefaultBmcCfg --clear_user_cfg --load_default_password | tee -a $LOG_FILE 
	[ $? -eq 0 ] || update_status=0


	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}

############
## INSPUR
## NF5588M4
############
function init_INSPUR_NF5588M4()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/NF5588M4/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE


	./sum_2.5.2_Linux_x86_64/sum -c LoadDefaultBiosCfg | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	./sum_2.5.2_Linux_x86_64/sum -c LoadDefaultBmcCfg --clear_user_cfg --load_default_password | tee -a $LOG_FILE 
	[ $? -eq 0 ] || update_status=0


	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}
############
## INSPUR
## SA5112M4
############
function init_INSPUR_SA5112M4()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/SA5112M4/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE

	echo "load default BMC" | tee -a $LOG_FILE
	echo y | ./socflash_x64 if=./SA5112M4_BMC_4.23.0_20181109  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "load default BIOS" | tee -a $LOG_FILE
	./step1.sh SA5112M4_BIOS_4.1.15_Tencent-1U_20180802.bin  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}

############
## INSPUR
## SA5112M5
############
function init_INSPUR_SA5112M5()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/SA5112M5/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE

	ipmitool raw 0x3c 0x3a 0x1e 0x01  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	echo "load default BMC" | tee -a $LOG_FILE
	echo y | ./socflash_x64 if=./SA5112M4_BMC_4.12.0_Tecent_20171030  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "load default BIOS" | tee -a $LOG_FILE
	./afulnx_64 SA5112M4_BIOS_4.1.17_20181204.bin /p /b /n /x /k /l /me  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}

############
## INSPUR
## SA5212M4
############
function init_INSPUR_SA5212M4()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/SA5212M4/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE

	echo "load default BMC" | tee -a $LOG_FILE
	echo y | ./socflash_x64 if=./SA5212M4_BMC_4.35.0_Standard_20191025 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "load default BIOS" | tee -a $LOG_FILE
	./step1.sh SA5212M4_BIOS_4.1.15_Tencent-2U_20180802.bin | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status


}

############
## INSPUR
## SA5212M5
############
function init_INSPUR_SA5212M5()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/SA5212M5/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE

	ipmitool raw 0x3c 0x3a 0x1e 0x00 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "load default BMC" | tee -a $LOG_FILE
	echo y | ./socflash_x64 if=./NF5280M5_BMC_4.27.2_Standard_20210624 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "load default BIOS" | tee -a $LOG_FILE
	./step1.sh NF5280M5_BIOS_4.1.18_Standard_20210121.bin |  tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}


############
## INVENTEC
## RM760_VI
############
function init_INVENTEC_RM760_IV()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Inventec_biosbmc_init_v1/RM760-IV/"
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE


	echo "load default BIOS" | tee -a $LOG_FILE
	ipmitool raw 0x3e 0x12 1 1  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "set BMC load default mode" | tee -a $LOG_FILE
	ipmitool raw 0x32 0xba 0 0 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "load default BMC" | tee -a $LOG_FILE
	ipmitool raw 0x32 0x66 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status
}

############
## SUGON
## A620_G30
############
function init_SUGON_A620_G30()
{
	update_status=1
	mypath="${BMC_ROOT_PATH}/Sugon_biosbmc_init_v1/A620-G30/"

	CURR_BIOS=`dmidecode -t bios |grep Version |awk -F':' '{print $2}' | awk '$1=$1'`
	cd $mypath
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE

	echo "load default BMC" | tee -a $LOG_FILE
	echo y | ./socflash_x64 if=./076WYSC010350T2.ima  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "load default BIOS" | tee -a $LOG_FILE
	if [[ "$CURR_BIOS" == "TWYS2012" ]];then
		echo "Current BIOS version is TWYS2012, update TWYS2012" |  tee -a $LOG_FILE
		echo E | ./afulnx_643 TWYS2012.ROM /b /p /n /k  |  tee -a $LOG_FILE
		[ $? -eq 0 ] || update_status=0

	else
		echo "Current BIOS version is TWYS2023, update TWYS2023"  |  tee -a $LOG_FILE
		echo E | ./afulnx_643 TWYS2023.ROM /b /p /n /k  |  tee -a $LOG_FILE
		[ $? -eq 0 ] || update_status=0
	fi


	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status
}

############
## Lenovo
## RD340X RD350X RD450X HR650X
############
function init_Lenovo_generic(){

	update_status=1

	echo "load default BIOS" | tee -a $LOG_FILE
	ipmitool raw 0x2e 0x2b 0xFF  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0
	sleep 2


	ipmitool raw 0x2e 0x2b 0 1  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "Cancel preserve config" | tee -a $LOG_FILE
	ipmitool raw 0x32 0xba 0x00 0x00 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0
	sleep 2

	echo "load default BMC, need 150s for bmc start" | tee -a $LOG_FILE
	ipmitool raw 0x32 0x66 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}

############
## Lenovo
## HR630X
############
function init_Lenovo_HR630X(){

	update_status=1

	echo "load default BIOS" | tee -a $LOG_FILE
	ipmitool raw 0x0 0x8 0x3 0x08  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0
	sleep 2

	ipmitool raw 0x0 0x8 0x5 0x80 0x80 0x0 0x0 0x0 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	echo "load default BMC" | tee -a $LOG_FILE
	ipmitool raw 0x32 0x66 | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	ipmitool sel clear  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0
	
	checkInitStatus $update_status
}

##########
# Inspur
##########
function boot_INSPUR(){

	productName=$1
	suportList=("$@")
	model=`getProductModel "$productName" "${INIT_SUPPORTLIST_INSPUR[@]}"`
	if [[ "$model" == '' ]] ||  [[ ! -d "${BMC_ROOT_PATH}/Inspur_biosbmc_init_v1/$model/" ]];then
		echo "Initialization of $productName[$model] is not supported." | tee -a $LOG_FILE
		exit 1
	fi

	init_INSPUR_${model}
}

##########
# Lenovo
##########
function boot_Lenovo(){

	productName=$1
	model=`getProductModel "$productName" "${INIT_SUPPORTLIST_LENOVO[@]}"`


	if [[ "$model" == '' ]] || [[ ! -d "${BMC_ROOT_PATH}/Lenovo_biosbmc_init_v1/$model/" ]];then
		echo "Initialization of $productName[$model] is not supported." | tee -a $LOG_FILE
		exit 1
	fi

	if [[ "$model" == "RD340X" ]] || [[ "$model" == "RD350X" ]] || [[ "$model" == "RD450X" ]] || [[ "$model" == "HR650X" ]] ;then
		init_Lenovo_generic
	fi

	if [ "$model" == "HR630X" ];then
		init_Lenovo_HR630X
	fi
}


##########
# Huawei
##########
function boot_Huawei(){

	cd ${BMC_ROOT_PATH}
	update_status=1
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE
	echo "load default BIOS" | tee -a $LOG_FILE

	./uniCfg6.0 -C | tee $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "delete ipmi user" | tee -a $LOG_FILE
	ipmitool user set name 3 '' | tee -a $LOG_FILE

	echo "load default BMC, need 150s for bmc start" | tee -a $LOG_FILE
	ipmitool raw 0x30 0x93 0xdb 0x07 0x00 0x08 0x00 0xaa  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	ipmitool sel clear | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}

##########
# HP
##########
function boot_HP(){

	update_status=1
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE	
	# add stonedev user
	#ipmi_add_user

	#ipmi_ip=`ipmi_getIP`
	#if [ $? -ne 0 ];then
	#	echo "unable to get ipmi ip." | tee -a $LOG_FILE	
	#	exit 1
	#fi

	echo "load default BIOS" | tee -a $LOG_FILE
    ilorest biosdefaults | tee -a $LOG_FILE
	#ilorest biosdefaults -u $IPMI_USER -p $IPMI_PASS --url $ipmi_ip | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	echo "load default BMC" | tee -a $LOG_FILE
	ilorest iloreset | tee -a $LOG_FILE
	#ilorest iloreset -u $user -p $passwd --url $ip | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0


	echo "delete ipmi user" | tee -a $LOG_FILE
	ipmitool user set name 3 '' | tee -a $LOG_FILE
	#ipmitool user set name 8 '' | tee -a $LOG_FILE

	ipmitool sel clear | tee -a $LOG_FILE

	checkInitStatus $update_status


}

##########
# DELL
##########
function boot_DELL(){

	IPMI_USER='root'
	IPMI_PASS='calvin'
	update_status=1
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE	
	# add stonedev user
	#ipmi_add_user
	ipmi_ip=`ipmi_getIP`
	if [ $? -ne 0 ];then
		echo "unable to get ipmi ip." | tee -a $LOG_FILE	
		exit 1
	fi

	## 重置ipmi 用户
	echo "reset ipmi default user" | tee -a $LOG_FILE
	ipmitool user set name 2 $IPMI_USER  | tee -a $LOG_FILE
	if [ $? -ne 0 ];then
		echo "failed to modify ipmi user." | tee -a $LOG_FILE
		exit 1
	fi

	## 重置ipmi 密码
	echo "reset ipmi default password" | tee -a $LOG_FILE
	ipmitool user set password 2 $IPMI_PASS  | tee -a $LOG_FILE
	if [ $? -ne 0 ];then
		echo "failed to modify ipmi password." | tee -a $LOG_FILE
		exit 1
	fi

	echo "load default BIOS" | tee -a $LOG_FILE
    sshpass -p ${IPMI_PASS} ssh -o "StrictHostKeyChecking no" ${IPMI_USER}@${ipmi_ip} "racadm set LifeCycleController.LCAttributes.BIOSRTDRequested 1" 2>&1 | tee -a $LOG_FILE
	if [ $? -ne 0 ];then
		update_status=0
		echo "load BIOS failed" | tee -a $LOG_FILE
	fi
	txt=`grep 'The BIOS reset to default values operation is pending a Server restart' $LOG_FILE`
	if [ $? -ne 0 ];then
		update_status=0
		echo "load BIOS failed" | tee -a $LOG_FILE
	fi

	echo "load default BMS" | tee -a $LOG_FILE
    sshpass -p ${IPMI_PASS} ssh -o "StrictHostKeyChecking no" ${IPMI_USER}@${ipmi_ip} "racadm   racresetcfg" 2>&1 | tee -a $LOG_FILE
	if [ $? -ne 0 ];then
		update_status=0
		echo "load BMS failed" | tee -a $LOG_FILE
	fi
	txt=`grep 'The RAC configuration has initiated restoration to factory' $LOG_FILE`
	if [ $? -ne 0 ];then
		update_status=0
		echo "load BMS failed" | tee -a $LOG_FILE
	fi

	ipmitool sel clear | tee -a $LOG_FILE

	checkInitStatus $update_status

}

##########
# INVENTEC
##########
function boot_INVENTEC(){
	
	productName=$1
	model=`getProductModel "$productName" "${INIT_SUPPORTLIST_INVENTEC[@]}"`
	if [[ "$model" == '' ]] ||  [[ ! -d "${BMC_ROOT_PATH}/Inventec_biosbmc_init_v1/$model/" ]];then
		echo "Initialization of $productName[$model] is not supported." | tee -a $LOG_FILE
		exit 1
	fi

	if [ "$model" == "RM760-IV" ];then
		init_INVENTEC_RM760_IV
	fi
}

##########
# SUGON
##########
function boot_SUGON(){
	productName=$1
	model=`getProductModel "$productName" "${INIT_SUPPORTLIST_SUGON[@]}"`
	if [[ "$model" == '' ]] ||  [[ ! -d "${BMC_ROOT_PATH}/Sugon_biosbmc_init_v1/$model/" ]];then
		echo "Initialization of $productName[$model] is not supported." | tee -a $LOG_FILE
		exit 1
	fi

	if [ "$model" == "A620-G30" ];then
		init_SUGON_A620_G30
	fi
}

##########
# FOXCONN
##########
function boot_FOXCONN(){

	cd ${BMC_ROOT_PATH}
	update_status=1
	echo "Start initializing BIOS and IPMI" | tee -a $LOG_FILE

	echo "load default BIOS" | tee -a $LOG_FILE
	ipmitool raw 0x0 0x08 0x05 0x80 0x80 0x0 0x0 0x0 | tee $LOG_FILEC
	[ $? -eq 0 ] || update_status=0


	echo "load default BMC, need 150s for bmc start" | tee -a $LOG_FILE
	ipmitool raw 0x32 0x66  | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	ipmitool sel clear | tee -a $LOG_FILE
	[ $? -eq 0 ] || update_status=0

	checkInitStatus $update_status

}

function main(){
	vendor=`dmidecode -t system |grep Manufacturer | awk '{print $2}'  | tr '[a-z]' '[A-Z]'`
	model=`dmidecode -t system |grep "Product Name" | awk -F':' '{print $2}'`
	sn=`dmidecode -t system |grep "Serial Number" | awk '{print $3}'`

	LOG_FILE="${LOG_FILE}_${sn}.log"

	> $LOG_FILE

	if [ -f /root/.ssh/known_hosts ];then
		rm /root/.ssh/known_hosts
	fi

	if [ "$vendor" == 'INSPUR' ];then
		boot_INSPUR "$model"
	fi

	if [ "$vendor" == 'LENOVO' ];then
		boot_Lenovo "$model"
	fi

	if [ "$vendor" == 'HUAWEI' ];then
		boot_Huawei "$model"
	fi

	if [ "$vendor" == "HP" ];then
		boot_HP
	fi

	if [ "$vendor" == "DELL" ];then
		boot_DELL
	fi

	if [ "$vendor" == 'SUGON' ];then
		boot_SUGON "$model"
	fi

	if [ "$vendor" == 'INVENTEC' ];then
		boot_INVENTEC "$model"
	fi

	if [ "$vendor" == 'FOXCONN' ];then
		boot_FOXCONN
	fi

	echo "$vendor is not supported." | tee -a $LOG_FILE
	exit 1

}

main